package cn.piesat.scanning.dao;


import cn.piesat.scanning.dto.DbDmsScanningMetadataAuxInfoDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 辅助数据持久层
 *
 * @author xhl
 * @Date 2020/8/15
 */
public interface DbDmsScanningMetadataAuxInfoDAO extends JpaRepository<DbDmsScanningMetadataAuxInfoDTO,String> {


    /**
     * 根据文件名称和文件大小查询辅助数据信息
     * @param fileName 文件名称
     * @param fileSize 文件大小
     * @return
     */
    DbDmsScanningMetadataAuxInfoDTO findByFileNameAndFileSize(String fileName,Long fileSize);

    /**
     * 根据气象要素和时间范围查询辅助数据集合
     * @param fileWant 气象要素
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Query(value = "select ID,FILE_NAME,FILE_WANT,FILE_WANT_REMARK,FILE_DATE,FILE_TIMESECOND,FILE_PATH,FILE_SIZE " +
            " from db_dms_scanning_metadata_aux_info where FILE_WANT = :fileWant and FILE_DATE between" +
            " :startTime and :endTime order by FILE_DATE,FILE_TIMESECOND desc ",nativeQuery = true)
    List<DbDmsScanningMetadataAuxInfoDTO> findByFileWantAndRangeTime(String fileWant,String startTime,String endTime);

    /**
     * 根据气象要素和时间范围查询辅助数据集合，时间+时次降序
     * @param wantList 气象要素列表
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Query(value = "select ID,FILE_NAME,FILE_WANT,FILE_WANT_REMARK,FILE_DATE,FILE_TIMESECOND,FILE_PATH,FILE_SIZE,FORECAST_DATE,FILE_CATALOG " +
            " from db_dms_scanning_metadata_aux_info where FILE_WANT in :wantList and FILE_DATE between" +
            " :startTime and :endTime order by FILE_DATE desc,FILE_TIMESECOND desc ",nativeQuery = true)
    List<DbDmsScanningMetadataAuxInfoDTO> findByFileWantListAndRangeTimeOrder(List<String> wantList, String startTime, String endTime);

    @Query(value = "from DbDmsScanningMetadataAuxInfoDTO t where t.fileWant in :fileWantList " +
            "t.fileDate between :startTime and :endTime order by FILE_DATE desc,FILE_TIMESECOND desc",nativeQuery = true)
    List<DbDmsScanningMetadataAuxInfoDTO> findAuxFileByFileWantAndRangeTime(@Param("fileWantList") List<String> fileWantList, @Param("startTime") String startTime, @Param("endTime") String endTime);

    /**
     * 根据时间范围查询所有文件名称
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Query(value = "select fileName from DbDmsScanningMetadataAuxInfoDTO where fileDate between :startTime and :endTime")
    List<String> findFileNameByDate(String startTime, String endTime);

    List<DbDmsScanningMetadataAuxInfoDTO> findByFileCatalogAndForecastDateBetweenOrderByForecastDateDesc(String fileCatalog,  String startTime, String mainFileDate1);

    List<DbDmsScanningMetadataAuxInfoDTO> findByFileCatalogAndForecastDateOrderByFileDateDesc(String fileCatalog, String forecastDate);
}
